package com.pearadmin.pro.modules.training.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.aop.annotation.Repeat;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.sys.param.SysConfigRequest;
import com.pearadmin.pro.modules.training.entity.LaboratoryInfo;
import com.pearadmin.pro.modules.training.entity.TrainingEquipment;
import com.pearadmin.pro.modules.training.entity.vo.PageTrainingEquipmentVO;
import com.pearadmin.pro.modules.training.service.LaboratoryInfoService;
import com.pearadmin.pro.modules.training.service.TrainingEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = {"实训设备"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "tec")
public class TrainingEquipmentController extends BaseController {

    @Resource
    private TrainingEquipmentService trainingEquipmentService;

    @Resource// 实训室
    private LaboratoryInfoService laboratoryInfoService;

    @Resource
    private UserContext userContext;

    @GetMapping("page")
    @Log(title = "实训室设备列表")
    @ApiOperation(value = "实训室设备列表")
    public Result page(PageTrainingEquipmentVO request){
        PageResponse<TrainingEquipment> page = trainingEquipmentService.page(request);
        List<TrainingEquipment> record = page.getRecord();
        record.forEach(trainingEquipment -> {
            Integer liId = trainingEquipment.getLiId();
            LaboratoryInfo byId = laboratoryInfoService.getById(liId);
            trainingEquipment.setLainName(byId.getLainName());
        });
        page.setRecord(record);
        return success(page);
    }
    /**
     * 新增实训室设备
     *
     * @param trainingEquipment
     */
    @Repeat
    @PostMapping("save")
    @Log(title = "新增实训室设备")
    @ApiOperation(value = "新增实训室设备")
    public Result save(@RequestBody TrainingEquipment trainingEquipment) {
        trainingEquipment.setCreateName(userContext.getUserId());
        trainingEquipment.setCreateTime(LocalDateTime.now());
        return auto(trainingEquipmentService.save(trainingEquipment));
    }
    @Repeat
    @PostMapping("Update")
    @Log(title = "修改实训室设备")
    @ApiOperation(value = "修改实训室设备")
    public Result Update(@RequestBody TrainingEquipment trainingEquipment) {
        trainingEquipment.setUpdateName(userContext.getUserId());
        trainingEquipment.setUpdateTime(LocalDateTime.now());
        return auto(trainingEquipmentService.updateById(trainingEquipment));
    }
    /**
     * 删除实训室设备
     *
     * @param id 实训室设备编号
     */
    @DeleteMapping("remove")
    @Log(title = "删除实训室设备")
    @ApiOperation(value = "删除实训室设备")
    public Result remove(@RequestParam String id){
        return auto(trainingEquipmentService.removeById(id));
    }

    /**
     * 删除实训室设备
     *
     * @param ids 实训室设备编号
     * */
    @DeleteMapping("removeBatch")
    @Log(title = "删除实训室设备")
    @ApiOperation(value = "删除实训室设备")
    public Result removeBatch(@RequestParam List<String> ids) { return auto(trainingEquipmentService.removeByIds(ids)); }


}
